import * as api from '../api/Account';
import { message } from 'antd/lib/index';
import checkStatus from '@/utils/status';

export default {
  namespace: 'Account',

  state: {
    data: {
      data: [],
      pagination: {},
    },
  },
  effects: {
    //请求用户列表
    *fetch({ payload }, { call, put }) {
      const response = yield call(api.accountInfo, payload);
      yield checkStatus(response);
      yield put({
        type: 'get',
        payload: response.msg,
      });
    },
    *withdrawRecord({ payload }, { call, put }) {
      const response = yield call(api.withdrawRecord, payload);
      yield checkStatus(response);
      yield put({
        type: 'setWithdrawRecord',
        payload: response.msg,
      });
    },
    *updatePwd({ payload, callback }, { call, put }) {
      const response = yield call(api.updatePwd, payload);
      yield checkStatus(response);
      if (callback) callback(response);
    },
    *accountUpload({ payload, callback }, { call, put }) {
      const response = yield call(api.accountUpload, payload);
      yield checkStatus(response);
      if (callback) callback(response);
    },

    *tradePwd({ payload, callback }, { call, put }) {
      const response = yield call(api.tradePwd, payload);
      yield checkStatus(response);
      if (callback) callback(response);
    },
    *sendCode({ payload, callback }, { call, put }) {
      const response = yield call(api.sendCode, payload);
      if (callback) callback(response);
    },
    *openAccount({ payload, callback }, { call, put }) {
      const response = yield call(api.openAccount, payload);
      if (callback) callback(response);
    },
    *delBackCard({ payload, callback }, { call, put }) {
      const response = yield call(api.delBackCard, payload);
      if (callback) callback(response);
    },
    *bandBackCard({ payload, callback }, { call, put }) {
      const response = yield call(api.bandBackCard, payload);
      if (callback) callback(response);
    },

    *bankList({ payload, callback }, { call, put }) {
      const response = yield call(api.bankList, payload);
      yield put({
        type: 'setBankList',
        payload: response.msg,
      });
      if (callback) callback(response);
    },
    *accountDatas({ payload, callback }, { call, put }) {
      const response = yield call(api.accountData, payload);
      console.log(response);
      yield put({
        type: 'setAccountData',
        payload: response.msg,
      });
      if (callback) callback(response);
    },
    *memberList({ payload, callback }, { call, put }) {
      const response = yield call(api.memberList, payload);
      yield put({
        type: 'setMemberList',
        payload: response.msg,
      });
      if (callback) callback(response);
    },

    *getEnterpriseDesc({ payload, callback }, { call, put }) {
      const response = yield call(api.enterpriseDetail, payload);
      yield put({
        type: 'enterpriseDesc',
        payload: response.msg,
      });
      if (callback) callback(response);
    },
    *enterpriseList({ payload, callback }, { call, put }) {
      const response = yield call(api.enterpriseList, payload);
      yield checkStatus(response);
      yield put({
        type: 'companyList',
        payload: response.msg,
      });
      if (callback) callback(response);
    },
  },

  reducers: {
    get(state, action) {
      state.pagination = {
        pageSize: action.payload.pageSize,
        current: action.payload.pageNo,
        total: action.payload.total,
      };
      return {
        ...state,
        data: action.payload,
      };
    },
    setWithdrawRecord(state, action) {
      return {
        ...state,
        withdrawRecordList: action.payload,
      };
    },
    enterpriseDesc(state, action) {
      return {
        ...state,
        companyDesc: action.payload,
      };
    },
    setBankList(state, action) {
      return {
        ...state,
        bankList: action.payload,
      };
    },
    setMemberList(state, action) {
      return {
        ...state,
        memberList: action.payload,
      };
    },

    edit(state, action) {
      if (action.payload.code === 200) {
        message.success('更新成功');
      } else {
        message.error(action.payload.errMsg);
      }
      return {
        ...state,
        data: action.payload,
      };
    },
    deptList(state, action) {
      return {
        ...state,
        deptList: action.payload,
      };
    },
    setdesc(state, action) {
      return {
        ...state,
        userDesc: action.payload,
      };
    },
    companyList(state, action) {
      return {
        ...state,
        companyList: action.payload,
      };
    },
    RoleList(state, action) {
      return {
        ...state,
        RoleLists: action.payload,
      };
    },
    setAccountData(state, action) {
      return {
        ...state,
        AccountDataList: action.payload,
      };
    },
  },
};
